package scripts;
import bot.ScriptMethods;

public class VarrockMiner extends ScriptMethods implements Runnable {

	public void start() {
		getAccountSettings();
		System.gc();
		scriptRunning = true;
		scriptThread = new Thread(this);
		scriptThread.start();
	}

	public void stop() {
		System.gc();
		scriptThread = null;
		scriptRunning = false;
		debug("Stopped script.");
	}

	public void run() {
		while (scriptRunning) {
			try {
				handleEvents();
				if (bankOpen()) {
					if (invContains(151)) { 
						depositAll(151);
						wait(600);
					}
					if (invContains(157)) { 
						depositAll(157);
						wait(random(600, 750));
					}
					if (invContains(158)) { 
						depositAll(158);
						wait(random(600, 750));
					}
					if (invContains(159)) { 
						depositAll(159);
						wait(random(600, 750));
					}
					if (invContains(160)) { 
						depositAll(160);
						wait(random(600, 750));
					}
					closeBank();
					wait(600);
				}

				int[] bankDoor = getObjectById(64);
				if (bankDoor[0] != -1) {
					atObject(bankDoor[1], bankDoor[2]);
					wait(random(1000, 1200));
				}

				if (getInventorySize() == 30) {
					if (!bankOpen()) {
						if (ourPlayerX() > 97 && ourPlayerY() > 509) {
							openBank();
							wait(random(2000, 3000));
						} else {
							walkToWait(74, 535);
							wait(200);
							walkToWait(84, 509);
							wait(200);
							walkToWait(103, 512);
							wait(200);
						}
					}
				} else {
					if (ourPlayerX() > 97 && ourPlayerY() > 509) {
							walkToWait(103, 512);
							wait(200);
							walkToWait(84, 509);
							wait(200);
							walkToWait(74, 535);
							wait(200);
					} else {
						int[] iron1 = getObjectById(102);
						int[] iron2 = getObjectById(103);
						if (!isSleeping()) {
							if (iron1[0] != -1) {
								atObject(iron1[1], iron1[2]);
								wait(random(600, 1000));
							} else if (iron2[0] != -1) {
								atObject(iron2[1], iron2[2]);
								wait(random(600, 1000));
							} else {
								wait(200);
							}
						}
					}
				}
				wait(150);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void handleEvents() throws Exception {
		if (welcomeBoxOpen())
			setWelcomeBox();
		if (!isLoggedIn())
			doLogin();
		if (getFatigue() > 85 && !isSleeping()) {
			wait(600);
			useItem(1263);
			wait(600);
		}
		if (getFatigueSleeping() < 10 && isSleeping())
			doSleeping();
		if (getFightMode() != fightMode)
			setFightMode();
	}
}